<?php

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Modules\User\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;

class ApiController extends Controller
{
    /**
     * @return User
     */
    public function getUser()
    {
        return Auth::user();
    }

    public function success($data = null, $extra = null)
    {
        $ret = [
            'status' => 0,
            'timestamp' => Carbon::now()->getTimestamp(),
        ];

        $ret['data'] = $data;

        if (!is_null($extra)) {
            $ret = array_merge($ret, $extra);
        }

        return response()->json($ret, 200, [], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    }

    protected function fail($message, $status = -1, $extra = null)
    {
        $ret = [
            'status' => $status,
            'timestamp' => Carbon::now()->getTimestamp(),
            'message' => $message
        ];

        if (!is_null($extra)) {
            $ret = array_merge($ret, $extra);
        }

        return response()->json($ret, 200, [], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    }

    public function curl_post($url, $data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $response = curl_exec($ch);
        curl_close($ch);
//        echo $response;
        return $response;
    }

    /**
     * 发生统一服务消息
     * @param $url
     * @param $data
     * @return string
     */
    public function uniformMessage($access_token, $data)
    {
        $data = json_encode($data);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=' . $access_token;
        $result = $this->curl_post($url, $data);
        $result = json_decode($result);
        if ($result->errcode == '0' && $result->errmsg == 'ok') {
            return 'success';
        } else {
            return 'fail';
        }
    }

    public function getAccessToken()
    {
        $appid = env("WECHAT_MINI_PROGRAM_APPID");
        $secret = env("WECHAT_MINI_PROGRAM_SECRET");
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $res = $this->curl_get($url);
        $res = json_decode($res, 1);
        return $res['access_token'];
    }

    public function curl_get($url)
    {
        $headers = array('User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36');
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_TIMEOUT, 20);
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

}
